package com.practice.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.practice.entity.Emp;
import com.practice.entity.MyReturn;
import com.practice.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author:高杰领
 * @date:2021/11/18 15:20
 * @description:springMVCController
 * @version:
 **/
@Controller
public class EmpMVCController {
    @Autowired
    private EmpService empService;
    @Autowired
    private MyReturn myReturn;

    @RequestMapping("/showEmp.do")
    public String showEmp() {
        return "emp";
    }

    @RequestMapping("/showAllEmp.do")
    @ResponseBody
    public MyReturn showAllEmp() {
        List<Emp> empList = empService.selectAll();
        myReturn.setCode(0);
        myReturn.setMsg("成功");
        myReturn.setData(empList);
        return myReturn;
    }

    @RequestMapping("/addEmp.do")
    @ResponseBody
    public MyReturn addEmp(Emp emp) {
        boolean insertEmp = empService.insertEmp(emp);
        if (insertEmp) {
            myReturn.setCode(0);
            myReturn.setMsg("成功");
        } else {
            myReturn.setCode(1);
            myReturn.setMsg("失败");
        }
        return myReturn;
    }

    @RequestMapping("/deleteEmp.do")
    @ResponseBody
    public MyReturn deleteEmp(int EMPNO) {
        boolean deleteEmp = empService.deleteEmp(EMPNO);
        if (deleteEmp) {
            myReturn.setCode(0);
            myReturn.setMsg("成功");
        } else {
            myReturn.setCode(1);
            myReturn.setMsg("失败");
        }
        return myReturn;
    }

}
